// pages/borrow/borrow.ts
import { Request } from '../../utils/util'
import Notify from '@vant/weapp/notify/notify';
const app = getApp()
Page({
    data: {
        navBarData: { // 导航栏的数据
            title: '器材借用',
            scan: false,
            navigateBack: true
        },
        keepdays: 0,
        borrowtime: 0,
        id: 0,
        borrownum: 0,
        startDay: "",
        endDay: "",
        startTime: '00:00',
        endTime: '23:59',
        deviceInfo: {},
        showEndCalendar: false,
        showStartCalendar: false,
        showStartTimePicker: false,
        showEndTimePicker: false,
        currentTime: '',
        amountList: [],
        borrowAmount: 0,
        loading:true
    },
    onLoad(option: any) {
        this.setData({id:option.id})
        Request({
            url: `/getdeviceinfo?id=${option.id}`,
        }).then(res => {
            this.setData({
                deviceInfo: res,
            })
            let amountList = []
            console.log("res.amount", this.data.deviceInfo)
            for (let i = 0; i < this.data.deviceInfo.amount; i++) {
                amountList.push({
                    index: i
                })
            }
            this.setData({ amountList: amountList })
            console.log("loadingdata over",this.data.amountList)
            // setTimeout() ,2000)

        }).catch(error => {
            console.log(error);
        });
        let date = new Date()
        let curDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
        let curTime
        let hour=date.getHours() >10?date.getHours() :`0${date.getHours()}`
        let minute=date.getMinutes() + 1>10?date.getMinutes() + 1:`0${date.getMinutes()}`
        this.setData({
            startDay: curDate,
            endDay: curDate,
            currentTime: `${hour}:${minute}`,
            startTime: `${hour}:${minute}`
        })
        
    },
    onReady(){
        this.setData({loading:false})
    },
    chooseAmount(e) {
        this.setData({ borrowAmount: e.target.dataset.index })
    },
    chooseDateEnd() {
        console.log("choseDare")
        this.setData({
            showEndCalendar: true,
        })
    },
    onCloseEnd() {
        this.setData({ showEndCalendar: false });
    },
    formatDate(date) {
        date = new Date(date);
        return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
    },
    onConfirmEnd(event) {
        this.setData({
            showEndCalendar: false,
            endDay: this.formatDate(event.detail),
        });
    },

    chooseDatStart() {
        this.setData({
            showStartCalendar: true,
        })
    },
    onCloseStart() {
        this.setData({ showStartCalendar: false });
    },
    onConfirmStart(event) {
        this.setData({
            showStartCalendar: false,
            startDay: this.formatDate(event.detail),
        });
    },
    chooseTimeStart() {
        this.setData({ showStartTimePicker: true })
    },

    chooseTimeEnd() {
        this.setData({ showEndTimePicker: true })
    },
    confirmStartTime(e) {
        this.setData({ startTime: e.detail })
    },
    cancelStartTime(e) {
        this.setData({ showEndTimePicker: false })
    },
    onClickHideStart() {
        this.setData({ showStartTimePicker: false })
    },
    confirmEndTime(e) {
        this.setData({ endTime: e.detail })
    },
    cancelEndTime(e) {
        this.setData({ showEndTimePicker: false })
    },

    onClickHideEnd() {
        this.setData({ showEndTimePicker: false })
    },
    submitBorrow(){
        console.log(this.data.endDay+' '+this.data.startTime+':00')
        let date=new Date(this.data.endDay+' '+this.data.startTime)
        console.log(date)
        let _this=this
        wx.showModal({
            title: '提示',
            content: '确认借用',
            success(res) {
                if (res.confirm) {
                    Request({
                        url: '/newborrow',
                        method:'post',
                        data:{
                            data:{
                                keepdays:new Date(_this.data.endDay+' '+_this.data.endTime),
                                borrowtime:new Date(_this.data.startDay+' '+_this.data.startTime),
                                id:_this.data.id,
                                borrownum:_this.data.borrowAmount+1
                            }
            
                        }
                    }).then(res => {
                        if(res.success==true){
                            Notify({
                                color: '#ffffff',
                                background: app.globalData.notifyColor,
                                message: '借用成功',
                                top: app.globalData.navBarHeight
                            });
                            setTimeout(function () {
                                wx.navigateBack()
                            }, 2000)
                        }else{
                            Notify({
                                color: '#ffffff',
                                background: app.globalData.notifyColor,
                                message: '借用失败',
                                top: app.globalData.navBarHeight
                            });
                        }
                    }).catch(error => {
                        console.log(error);
                    });
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
 
    }
})